import { defineStore } from "pinia";
import { setOrderNo, setToken as authSetToken, setUserInfo as authSetUserInfo, getToken, getUserInfo } from '@/utils/auth.js'



// pinia 在服务端就渲染了，导致获取不到cookie，所以没法从cookie中取值，然后赋值给state对象

let token = getToken()
let userInfo = getUserInfo()

// console.log('token:', token)
// console.log('userInfo:', userInfo)

export const loadStore = defineStore("loadStore", {
  state() {
    return {
      remainNum: 0,
      userInfo: userInfo,
      token: token,
      vipNum: 0,
    }
  },
  actions: {
    
    setToken(token) {
      this.token = token
      authSetToken(token)
    },
    setUserInfo(userInfo) {
      this.userInfo = userInfo
      authSetUserInfo(userInfo)
    },
    loginOut() {
      this.userInfo = null
      this.token = ''
      authSetToken('')
      authSetUserInfo(null)
    },
    setVipNum (num) {
      this.vipNum = num
    }
  },
  persist: true,
})